import { ref } from 'vue'
import { defineStore } from 'pinia'

export const useCounterStore = defineStore('counter', () => {
  const cart = ref([])
  function jr(cartData,goodsid) {
     let username=localStorage.getItem('username')
     let password=cart.value.filter(item=>item.username==username)
     let name=password.find(item=>item.goods.goods_id==goodsid)
     if(name)
     {
       name.number++;
     }
     else
     {
       cart.value.push({
        username:username,
        id:Math.floor(Math.random()*100000),
        number:1,
        goods:cartData,
        checked:false
       })
     }
  }
    function sc(id){
         cart.value=cart.value.filter(item=>item.id!=id)
    }
  return { cart,jr, sc }
},
  // 持久化
   {
    persist: true,
  },
)
